home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / usr / share / initramfs-tools / hook-functions next >
Text File  |  2008-06-26  |  11KB  |  439 lines

  1. # -*- shell-script -*-
  2.  
  3. catenate_cpiogz() {
  4.     # Sanity check
  5.     if [ ! -e "${1}" ]; then
  6.         echo "W:catenate_cpiogz: arg1='${1}' does not exist." >&2
  7.         return
  8.     fi
  9.  
  10.     cat "${1}" >>"${__TMPCPIOGZ}"
  11. }
  12.  
  13. force_load()
  14. {
  15.         manual_add_modules ${@}
  16.         echo "${@}" >>"${DESTDIR}/conf/modules"
  17. }
  18.  
  19. # Takes a file containing a list of modules to be added as an
  20. # argument, figures out dependancies, and adds them.
  21. #
  22. # Input file syntax:
  23. #
  24. #   # comment
  25. #   modprobe_module_name [args ...]
  26. #   [...]
  27. #
  28. add_modules_from_file()
  29. {
  30.     # Sanity check
  31.     if [ ! -e "${1}" ]; then
  32.         echo "W:add_modules_from_file: arg1='${1}' does not exist." >&2
  33.         return
  34.     fi
  35.  
  36.     sed -e '/^#/d' ${1} | while read module rest; do
  37.         force_load "${module}" "${rest}"
  38.     done
  39. }
  40.  
  41. # Add dependent modules + eventual firmware
  42. manual_add_modules()
  43. {
  44.     local mam_x firmwares firmware
  45.  
  46.     for mam_x in $(modprobe --set-version="${version}" --ignore-install \
  47.     --show-depends "${1}" 2>/dev/null | awk '/^insmod/ { print $2 }'); do
  48.         # Prune duplicates
  49.         if [ -e "${DESTDIR}/${mam_x}" ]; then
  50.             continue
  51.         fi
  52.  
  53.         mkdir -p "${DESTDIR}/$(dirname "${mam_x}")"
  54.         ln -s "${mam_x}" "${DESTDIR}/$(dirname "${mam_x}")"
  55.         if [ "${verbose}" = "y" ]; then
  56.             echo "Adding module ${mam_x}"
  57.         fi
  58.  
  59.         # Add firmware files if necessary
  60.         firmwares=$(modinfo -F firmware "${mam_x}")
  61.         if [ -z "${firmwares}" ]; then
  62.             continue
  63.         fi
  64.         for firmware in $firmwares; do
  65.             if [ -e "${DESTDIR}/lib/firmware/${version}/${firmware}" ]; then
  66.                 continue
  67.             fi
  68.  
  69.             # Only print warning for missing fw of loaded module
  70.             # or forced loaded module
  71.             if [ ! -e "/lib/firmware/${version}/${firmware}" ]; then
  72.                 if grep -q "^$(basename "${mam_x}" .ko)" \
  73.                 /proc/modules \
  74.                 || grep -q "^$(basename "${mam_x}" .ko)" \
  75.                 "${CONFDIR}/modules"; then
  76.                     echo "W: Possible missing firmware /lib/firmware/${version}/${firmware} for module $(basename ${mam_x} .ko)" >&2
  77.                 fi
  78.                 continue
  79.             fi
  80.  
  81.             copy_exec "/lib/firmware/${version}/${firmware}"
  82.             if [ "${verbose}" = "y" ]; then
  83.                 echo "Adding firmware ${firmware}"
  84.             fi
  85.         done
  86.     done
  87. }
  88.  
  89. # $1 is the source path (e.g. /usr/bin/time)
  90. # $2 is the relative destination (e.g. /usr or /usr/time)
  91. #
  92. # The destination is interpreted in the same way "cp" would, meaning
  93. # (assuming /bin is a directory):
  94. #
  95. #   "copy_exec /usr/bin/time /bin"        -> /bin/time
  96. #   "copy_exec /usr/bin/time /bin/mytime" -> /bin/mytime
  97. # If $2 is left out, the same destination path as for the source arg will
  98. # be used and directories will be created as needed, so:
  99. #
  100. #   "copy_exec /usr/bin/time"             -> /usr/bin/time
  101. #
  102. copy_exec() {
  103.     local source target destination final_destination x nonoptlib
  104.     local libname dirname
  105.  
  106.     source="${1}"
  107.     if [ -n "${2}" ]; then
  108.         target="${2}"
  109.     else
  110.         if [ ! -e "${DESTDIR}/$(dirname "${1}")" ]; then
  111.             mkdir -p "${DESTDIR}/$(dirname "${1}")"
  112.         fi
  113.         target="${1}"
  114.     fi
  115.  
  116.     if [ -d "${DESTDIR}/${target}" ]; then
  117.         destination="${target}/$(basename "${source}")"
  118.     else
  119.         destination="${target}"
  120.     fi
  121.     final_destination="${DESTDIR}/${destination}"
  122.  
  123.     if [ -L "$final_destination" ]; then
  124.         if [ $(readlink "${final_destination}") != "${source}" ]; then
  125.             echo "W:copy_exec: Not copying ${source} to \$DESTDIR${destination}, which is already a copy of $(readlink ${final_destination})" >&2
  126.             return
  127.         fi
  128.     else
  129.         ln -s ${source} ${DESTDIR}/${destination}
  130.         if [ "${verbose}" = "y" ]; then
  131.             echo "Adding binary ${source}"
  132.         fi
  133.     fi
  134.  
  135.     # Copy the dependant libraries
  136.     for x in $(ldd ${source} 2>/dev/null | sed -e '
  137.         /\//!d;
  138.         /linux-gate/d;
  139.         /=>/ {s/.*=>[[:blank:]]*\([^[:blank:]]*\).*/\1/};
  140.         s/[[:blank:]]*\([^[:blank:]]*\) (.*)/\1/' 2>/dev/null); do
  141.  
  142.         # Try to use non-optimised libraries where possible.
  143.         # We assume that all HWCAP libraries will be in tls.
  144.         nonoptlib=$(echo "${x}" | sed -e 's#/lib/\(tls\|i686\).*/\(lib.*\)#/lib/\2#')
  145.  
  146.         if [ -e "${nonoptlib}" ]; then
  147.             x="${nonoptlib}"
  148.         fi
  149.  
  150.         libname=$(basename "${x}")
  151.         dirname=$(dirname "${x}")
  152.  
  153.         mkdir -p "${DESTDIR}/${dirname}"
  154.         if [ ! -e "${DESTDIR}/${dirname}/${libname}" ]; then
  155.             ln -s "${x}" "${DESTDIR}/${dirname}"
  156.             if [ "${verbose}" = "y" ]; then
  157.                 echo "Adding library ${x}"
  158.             fi
  159.         fi
  160.     done
  161. }
  162.  
  163. # Copy entire subtrees to the initramfs
  164. copy_modules_dir()
  165. {
  166.     local x_mod
  167.  
  168.     if ! [ -d "${MODULESDIR}/${1}" ]; then
  169.         return;
  170.     fi
  171.     if [ "${verbose}" = "y" ]; then
  172.         echo "Copying module directory ${1}"
  173.     fi
  174.     for x_mod in $(find "${MODULESDIR}/${1}" -name '*.ko' -print); do
  175.         manual_add_modules $(basename ${x_mod} .ko)
  176.     done
  177. }
  178.  
  179. # walk /sys for relevant modules
  180. sys_walk_mod_add()
  181. {
  182.     local driver_path module
  183.     device_path="$1"
  184.     
  185.     while [ "${device_path}" != "/sys" ]; do
  186.         driver_path="$(readlink -f ${device_path}/driver)"
  187.         if [ -e "$driver_path" ]; then
  188.             module="$(basename $(readlink -f $driver_path))"
  189.             if [ -n "${module}" ]; then
  190.                 force_load "${module}"
  191.             fi
  192.         fi
  193.         device_path="$(dirname ${device_path})"
  194.     done
  195. }
  196.  
  197. # walk /sys for relevant modalias
  198. sys_walk_modalias()
  199. {
  200.     local device_path modalias
  201.  
  202.     device_path="$(dirname "${1}")"
  203.     device_path="$(dirname "${device_path}")"
  204.     if [ -e "${device_path}/modalias" ]; then
  205.         modalias=$(cat "${device_path}/modalias")
  206.     fi
  207.  
  208.     if [ -n "${modalias}" ]; then
  209.         force_load "${modalias}"
  210.     fi
  211. }
  212.  
  213. # find and only copy root relevant modules
  214. dep_add_modules()
  215. {
  216.     local block minor root FSTYPE root_dev_path x
  217.  
  218.     # findout root block device + fstype
  219.     eval "$(mount | awk '/\/dev\// {if ($3 == "/") {print "root=" $1 "\nFSTYPE=" $5; exit}}')"
  220.     if [ "${root}" = "/dev/root" ] ; then
  221.         root="/dev/disk/by-uuid/"$(/lib/udev/vol_id --uuid ${root}) 2>/dev/null
  222.     fi
  223.     root="$(readlink -f ${root})"
  224.  
  225.     # find out real rootfs on auto type
  226.     if [ "${FSTYPE}" = "auto" ]; then
  227.         eval "$(/usr/lib/klibc/bin/fstype ${root})"
  228.     fi
  229.  
  230.     # check that fstype rootfs recognition
  231.     if [ "${FSTYPE}" = "unknown" ]; then
  232.         echo "mkinitramfs: unknown fstype on root ${root}"
  233.         echo "mkinitramfs: workaround is MODULES=most" 
  234.         echo "mkinitramfs: Error please report bug on initramfs-tools" 
  235.         exit 1
  236.     fi
  237.  
  238.     # Add rootfs
  239.     manual_add_modules "${FSTYPE}"
  240.  
  241.     # lvm luks root
  242.     if [ "${root#/dev/mapper/}" != "${root}" ]; then
  243.         minor=$((0x$(stat --format "%T" ${root}) % 256))
  244.         block=$(ls -1 /sys/block/dm-${minor}/slaves | head -n 1)
  245.         if [ "${block#dm-}" != "${block}" ]; then
  246.             block=$(ls -1 /sys/block/${block}/slaves | head -n 1)
  247.         fi
  248.         block=${block%[0-9]*}
  249.     # md root new naming scheme /dev/md/X
  250.     elif [ "${root#/dev/md/}" != "${root}" ]; then
  251.         root=${root#/dev/md/}
  252.         block=$(awk "/^md${root}/{print substr(\$5, 1, 3); exit}" \
  253.             /proc/mdstat)
  254.     # md root /dev/mdX
  255.     elif [ "${root#/dev/md}" != "${root}" ]; then
  256.         root=${root#/dev/}
  257.         block=$(awk "/^${root}/{print substr(\$5, 1, 3); exit}" \
  258.             /proc/mdstat)
  259.     # classical root device
  260.     else
  261.         block=${root#/dev/}
  262.         block=${block%[0-9]*}
  263.     fi
  264.  
  265.     # Error out if /sys lack block dev
  266.     if [ -z "${block}" ] || [ ! -e /sys/block/${block} ]; then
  267.         echo "mkinitramfs: missing ${block} root ${root} /sys entry"
  268.         echo "mkinitramfs: workaround is MODULES=most" 
  269.         echo "mkinitramfs: Error please report the bug" 
  270.         exit 1
  271.     fi
  272.  
  273.     # sys walk ATA
  274.     root_dev_path=$(readlink -f /sys/block/${block}/device)
  275.     sys_walk_mod_add ${root_dev_path}
  276.  
  277.     # catch old-style IDE
  278.     if [ -e /sys/bus/ide/devices/ ]; then
  279.         sys_walk_modalias ${root_dev_path}
  280.         manual_add_modules ide-disk
  281.         manual_add_modules ide-cd
  282.     fi
  283.  
  284.     if [ -e /sys/bus/scsi/devices/ ]; then
  285.         manual_add_modules sd_mod
  286.     fi
  287.  
  288.     if [ -e /sys/bus/i2o/devices/ ]; then
  289.         force_load i2o_block
  290.         force_load i2o_config
  291.     fi
  292.  
  293.     if [ -e /sys/bus/ps3_system_bus/ ]; then
  294.         for x in ps3disk ps3rom ps3-gelic ps3_sys_manager; do
  295.             manual_add_modules "${x}"
  296.         done
  297.     fi
  298.  
  299.     if [ -e /sys/bus/vio/ ]; then
  300.         for x in sunvnet sunvdc; do
  301.             manual_add_modules "${x}"
  302.         done
  303.     fi
  304. }
  305.  
  306.  
  307. # The modules "most" classes added per default to the initramfs
  308. auto_add_modules()
  309. {
  310.     case "$1" in
  311.     base)
  312.         for x in ehci-hcd ohci-hcd uhci-hcd usbhid usb-storage ext2 \
  313.         ext3 isofs jfs nfs reiserfs udf xfs af_packet atkbd i8042 \
  314.         virtio_pci vfat nls_cp437 nls_iso8859-1; do
  315.             manual_add_modules "${x}"
  316.         done
  317.     ;;
  318.     net)
  319.         for x in 3c59x 8139cp 8139too 8390 atl1 b44 bmac \
  320.         bnx2 cxgb3 defxx dl2k e100 e1000 e1000e ehea epic100 \
  321.         ep93xx_eth eql fealnx famachi forcedeth gelic_net \
  322.         hp100 igb ipg mace mv643xx_eth myri10ge \
  323.         natsemi ne2k-pci netconsole niu ns83820 pcnet32 qla3xxx \
  324.         r8169 s2io sis900 skge sky2 slhc smc911x starfire \
  325.         sundance sungem sungem_phy sunhme sunvnet tg3 tlan de2104x \
  326.         de4x5 dmfe tulip winbond-840 xircom_cb xircom_tulip_cb \
  327.         typhon via-rhine via-velocity yellowfin; do
  328.             manual_add_modules "${x}"
  329.         done
  330.     ;;
  331.     ide)
  332.         copy_modules_dir kernel/drivers/ide
  333.     ;;
  334.     scsi)
  335.         copy_modules_dir kernel/drivers/scsi
  336.         for x in mptfc mptsas mptscsih mptspi; do
  337.             manual_add_modules "${x}"
  338.         done
  339.     ;;
  340.     ata)
  341.         copy_modules_dir kernel/drivers/ata
  342.     ;;
  343.     block)
  344.         copy_modules_dir kernel/drivers/block
  345.     ;;
  346.     # FIXME: can be removed after Lenny release
  347.     ieee1394)
  348.         for x in ohci1394 sbp2; do
  349.             manual_add_modules "${x}"
  350.         done
  351.     ;;
  352.     firewire)
  353.         for x in firewire-ohci  firewire-sbp2; do
  354.             manual_add_modules "${x}"
  355.         done
  356.     ;;
  357.     i2o)
  358.         for x in i2o_block; do
  359.             manual_add_modules "${x}"
  360.         done
  361.     ;;
  362.     dasd)
  363.         for x in dasd_eckd_mod dasd_fba_mod; do
  364.             manual_add_modules "${x}"
  365.         done
  366.     ;;
  367.     *)
  368.         auto_add_modules base
  369.         auto_add_modules net
  370.         auto_add_modules ide
  371.         auto_add_modules scsi
  372.         auto_add_modules block
  373.         auto_add_modules ata
  374.         auto_add_modules i2o
  375.         auto_add_modules dasd
  376.         auto_add_modules ieee1394
  377.         auto_add_modules firewire
  378.     ;;
  379.     esac
  380. }
  381.  
  382. usage()
  383. {
  384.     cat >&2 << EOF
  385.  
  386. Usage: ${0} [OPTION]... <-o outfile> [version]
  387.  
  388. Options:
  389.   -d confdir  Specify an alternative configuration directory.
  390.   -k          Keep temporary directory used to make the image.
  391.   -o outfile  Write to outfile.
  392.   -r root     Override ROOT setting in mkinitrd.conf.
  393.  
  394. See mkinitramfs(8) for further details.
  395. EOF
  396.     exit 1
  397.  
  398. }
  399.  
  400. # minimal supported kernel version
  401. check_minkver()
  402. {
  403.     local curversion initdir DPKG_ARCH minversion cm_x tmp
  404.  
  405.     curversion="${1}"
  406.     initdir="${2}"
  407.     if [ -z "${initdir}" ]; then
  408.         DPKG_ARCH=$(dpkg --print-installation-architecture)
  409.         case ${DPKG_ARCH} in
  410.             ia64|hppa)
  411.                 minversion="2.6.15"
  412.             ;;
  413.             *)
  414.                 minversion="2.6.12"
  415.             ;;
  416.         esac
  417.         if dpkg --compare-versions "${curversion}" lt "${minversion}"; then
  418.             echo "W: kernel ${curversion} too old for initramfs on ${DPKG_ARCH}" >&2
  419.             echo "W: not generating requested initramfs for kernel ${curversion}" >&2
  420.             exit 2
  421.         fi
  422.         return 0
  423.     fi
  424.     set_initlist
  425.     for cm_x in ${initlist}; do
  426.         # sed: keep last line starting with MINKVER=,
  427.         #      remove MINKVER= and trailing space
  428.         minver=$(sed '/^MINKVER=/!d;$!d;s/^MINKVER=//;s/[[:space:]]*$//' "${initdir}/${cm_x}")
  429.         if [ -z "${tmp}" ]; then
  430.             continue
  431.         elif dpkg --compare-versions "${curversion}" lt "${minver}"; then
  432.             echo "W: ${cm_x} hook script requires at least kernel version ${minver}" >&2
  433.             echo "W: not generating requested initramfs for kernel ${curversion}" >&2
  434.             exit 2
  435.         fi
  436.     done
  437. }
  438.